//
//  TTDiscoverAlbumViewController.m
//  Adorable_Things
//
//  Created by lanou on 16/1/11.
//  Copyright © 2016年 Banana Inc. All rights reserved.
//

#import "TTDiscoverAlbumViewController.h"
#import "TTDiscoverAlbumCollectionViewCell.h"
#import "TTDiscoverAlbumModel.h"
#import "TTDiscoverArticleViewController.h"
#import "AlbumDetailViewController.h"
#define kAlbumUrl @"http://api.menghuoapp.com/v1/album/list"

@interface TTDiscoverAlbumViewController () <UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, strong) NSMutableArray *dataArr;
@property (nonatomic, assign) NSInteger requestStart;

@end

@implementation TTDiscoverAlbumViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.collectionView.backgroundColor = [UIColor whiteColor];
    
    _requestStart = 0;
    _dataArr = [NSMutableArray array];
    
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"LaunchImage-568h@2x"]];

    UINib *nib = [UINib nibWithNibName:@"TTDiscoverAlbumCollectionViewCell" bundle:nil];
    [self.collectionView registerNib:nib forCellWithReuseIdentifier:@"AlbumCollectionViewCell"];
    [self setUpData];
    
    self.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _requestStart = 0;
        [self setUpData];
    }];
    
    self.collectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        _requestStart += 1;
        [self setUpData];
    }];

}

- (void)setUpData
{
    NSMutableDictionary *parDic = [NSMutableDictionary dictionary];
    parDic[@"page"] = [NSString stringWithFormat:@"%ld", (long)_requestStart];
    parDic[@"page_size"] = @"20";
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager POST:kAlbumUrl parameters:parDic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSArray *dataArray = responseObject[@"data"];
        // 刷新的时候 有了新数据要把老数据清空，否则会造成数据重复
        if (_requestStart == 0) {
            [_dataArr removeAllObjects];
        }
        for (NSDictionary *oneDic in dataArray) {
            TTDiscoverAlbumModel *model = [[TTDiscoverAlbumModel alloc] init];
            [model setValuesForKeysWithDictionary:oneDic];
            [_dataArr addObject:model];
        }
        [self.collectionView.mj_header endRefreshing];
        [self.collectionView.mj_footer endRefreshing];
        [_collectionView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"请求失败");
    }];

}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _dataArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    TTDiscoverAlbumCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"AlbumCollectionViewCell" forIndexPath:indexPath];
    TTDiscoverAlbumModel *model = _dataArr[indexPath.row];
    [cell.mainImageV sd_setImageWithURL:[NSURL URLWithString:model.pic_url]];
    cell.mainImageV.layer.masksToBounds = YES;
    cell.mainImageV.layer.cornerRadius = 5;
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    AlbumDetailViewController *albumVC = [[AlbumDetailViewController alloc] init];
    TTDiscoverAlbumModel *model = _dataArr[indexPath.row];
    albumVC.albumID = model.album_id;
    albumVC.AlbumTitle = model.title;
    self.tabBarController.tabBar.hidden = YES;
    [self.navigationController pushViewController:albumVC animated:YES];
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
